package com.yhd.task.util;

import lombok.Data;

/**
 * 统一返回结果
 *
 * @author yinhuidong3@xiaomi.com
 * @since 2021/7/4
 */
@Data
public class Result<T> {

    
    private Integer code;


    private String message;


    private T data;

    public Result(){}

    // 返回数据
    protected static <T> Result<T> build(T data) {
        Result<T> result = new Result<T>();
        if (data != null)
            result.setData(data);
        return result;
    }

    public static <T> Result<T> build(T body, ResultCode ResultCode) {
        Result<T> result = build(body);
        result.setCode(ResultCode.getCode());
        result.setMessage(ResultCode.getMessage());
        return result;
    }

    public static<T> Result<T> SUCCESS(){
        return Result.SUCCESS(null);
    }


    public static<T> Result<T> SUCCESS(T data){
        Result<T> result = build(data);
        return build(data, ResultCode.SUCCESS);
    }

    public static<T> Result<T> ERROR(){
        return Result.ERROR(null);
    }


    public static<T> Result<T> ERROR(T data){
        Result<T> result = build(data);
        return build(data, ResultCode.FAIL);
    }

    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }

    public boolean isOk() {
        if(this.getCode().intValue() == ResultCode.SUCCESS.getCode().intValue()) {
            return true;
        }
        return false;
    }




}
